home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / BOSS_SUP.ARJ / WN_HELP.C < prev    next >
C/C++ Source or Header  |  1991-03-15  |  11KB  |  299 lines

  1. /*
  2. ** HELP - Help driver for The Window Boss
  3. **
  4. ** Copyright (c) 1985 - 1990 - Philip A. Mongelluzzo
  5. ** All rights reserved.
  6. **
  7. ** Comments:
  8. **
  9. **  Help files must be formatted properly!
  10. **
  11. **  Use GENINDEX to create the index file.
  12. **
  13. */
  14.  
  15. /* Header files */
  16.  
  17. #include "winboss.h"
  18. #include "math.h"                       /* needed for atol */
  19.  
  20. /* Prototypes for this function */
  21.  
  22. #if CI86 | DLC
  23. #else
  24. static  void showpage(long offset);
  25. static  int filbuf(long offset);
  26. #endif
  27.  
  28. /* Equates & definitions */
  29.  
  30. #if AZTEC
  31. #define RD "r"
  32. #else
  33. #define RD "rb"                         /* file access mode */
  34. #endif
  35.  
  36. #if CI86                                /* Compiler specific stuff */
  37. #define atol atoi                       
  38. extern FILE *fopen();                   /* Stream I/O */
  39. extern char *fgets();                   /* file line input */
  40. extern char *strcpy();                  /* string copy */
  41. extern char *strcat();                  /* concat function */
  42. extern long atol();                     /* ascii to integer */
  43. extern int fseek();                     /* DASD seek */
  44. extern long ftell();                    /* where are we DASD */
  45. #endif
  46.  
  47. static struct list {                    /* master list structure */
  48.   char *key;                            /* pointer to %info string */
  49.   long loc;                             /* file offset */
  50.   int type;                             /* type <0=toc; 1=page; 2=menu> */
  51. } master[HLPMAXKY];                     /* master ISAM structure */
  52.  
  53. #if CI86
  54. static char **scrbuf = NUL;             /* screen buffer (array of ptrs) */
  55. static char *line = NUL;                /* line buffer */
  56. #else
  57. static char **scrbuf = (char **) NULPTR;/* screen buffer (array of ptrs) */
  58. static char *line = (char *) NULPTR;    /* line buffer */
  59. #endif
  60. static initflg = FALSE;                 /* init complete flag */
  61. static int hrow, hcol;                  /* help window origin */
  62. static int hwidth, hheight;             /* help window size */
  63. static int hlsize;                      /* help line size */
  64. static unsigned int hwatr, hbatr;       /* help window atributes */
  65.  
  66. static int cpg, eoh;                    /* clear screen, end of help flags */
  67. static FILE *fp;                        /* stream file pointer */
  68.  
  69. static WINDOWPTR wn1;                   /* window pointer */
  70. static int row, col;                    /* original cursor location */
  71.  
  72. #ifdef COMMENTS
  73. /*
  74. ************
  75. * showpage *                            /* internal */        
  76. ************
  77. */
  78. #endif
  79.  
  80. static void showpage(offset)            /* display page function */
  81. long offset;
  82. {
  83. int i,j;                                /* scratch integers */
  84. long pages[HLPMAXKY];                   /* stack of pages */
  85. int pndx;                               /* stack pointer */
  86. int keyflg;                             /* vaild key flag */
  87.  
  88.   pndx = 0;                             /* init stack pointer */
  89.   pages[0] = offset;                    /* initial page */
  90.  
  91.   while(TRUE) {                         /* till forever ... */
  92.     i = filbuf(offset);                 /* fill the buffer */
  93.     for(j=0; j<i; j++)                  /* display buffer */
  94.       wn_puts(wn1,j,0,scrbuf[j]);
  95.     if(eoh) 
  96.       wn_putsa(wn1, 22, 0, " End of help, PgUp for previous screen, any other key to continue...", (wn1->style | BOLD));
  97.     else
  98.       wn_putsa(wn1, 22, 0, " Esc to quit help, PgUp for previous screen, any other key to continue...", (wn1->style | BOLD));
  99.     keyflg = FALSE;                     /* valid key flag */
  100.     do {                                /* loop till vaild key */
  101.       switch (v_getch() ) {             /* based on user input */
  102.         case PGUP:                      /* do all the right things */
  103.           pndx--;
  104.           if(pndx <0) {
  105.             wn_close(wn1);
  106.             v_locate(0, row, col);
  107.             return;
  108.           }
  109.           offset = pages[pndx];
  110.           keyflg = TRUE;
  111.           eoh = FALSE;                  /* force clear screen */
  112.           cpg = TRUE;                   /* and cancel end of help flag */
  113.           break;
  114.         case ESCAPE:
  115.           wn_close(wn1);
  116.           v_locate(0, row, col);
  117.           return;
  118.         default:
  119.           offset = pages[++pndx] = ftell(fp);
  120.           keyflg = TRUE;
  121.           break;
  122.       }
  123.     } while (!keyflg);                  /* loop till valid key */
  124.     if(cpg) wn_clr(wn1);
  125.     if(eoh) {
  126.       wn_close(wn1);
  127.       v_locate(0, row, col);
  128.       return;
  129.     }
  130.   }
  131. }
  132.  
  133. #ifdef COMMENTS
  134. /*
  135. **********
  136. * filbuf *                              /* internal */
  137. **********
  138. */
  139. #endif
  140.  
  141. static int filbuf(offset)               /* fill screen buffer */
  142. long offset;
  143. {
  144. int i;
  145. char *p, *p1;
  146.  
  147.   i=0;                                  /* initialize */
  148.   cpg = eoh = FALSE;                    /* assume nothing */
  149.   fseek(fp, offset, 0);                 /* position file */
  150.   do {
  151.     fgets(line, hlsize, fp);            /* fetch a line */
  152.       cpg = !strncmp(".cp", line,3);    /* lite clear screen and */
  153.       eoh = !strncmp("*END*\n",line,5); /* end of help flags */
  154.       if(cpg || eoh) break;
  155.       p = &line[0];                     /* a long way around to */
  156.       p1 = scrbuf[i];                   /* filter the carriage */
  157.       while (*p) {                      /* returns !! */
  158.         switch (*p) {
  159.           case '\r':
  160.           case '\n':
  161.             p++;
  162.             break;
  163.           default:
  164.             *p1++ = *p++;
  165.             break;
  166.         }
  167.       }
  168.       *p1 = '\0';
  169.       i++;
  170.   } while (TRUE);
  171.   return(i);                            /* return the # of lines on page */
  172. }
  173.  
  174. /*
  175. *************
  176. * wn_hlinit *
  177. *************
  178. */
  179.  
  180. wn_hlinit(row,col,width,height,atrib,batrib,help_file_name)
  181. int row, col;                           /* origin of help window */
  182. int width, height;                      /* width and height */
  183. unsigned int atrib, batrib;             /* window & boarder attributes */
  184. char *help_file_name;                   /* help file name */
  185. {
  186.   if(initflg) {                         /* already called ?? */
  187.     if(initflg == -2) return(FALSE);    /* died last time ?? */
  188.     return(TRUE);                       /* avoid foolishness */
  189.   }
  190.  
  191.   hrow = row;                           /* origin */
  192.   hcol = col;
  193.   hwidth = width;                       /* window size */
  194.   hheight = height;       
  195.   hlsize = width+2;                     /* line size (width+2 for border) */
  196.   hwatr = atrib;                        /* attributes */
  197.   hbatr = batrib;
  198.  
  199.   scrbuf = malloc((height+2) * sizeof(char *) * hlsize); 
  200.   if(!scrbuf) {
  201.     wns_errcod = 1;
  202.     return(FALSE);
  203.   }
  204.   line = malloc((hlsize) * sizeof(char)); 
  205.   if(!line) {
  206.     wns_errcod = 2;
  207.     return(FALSE);
  208.   }
  209.   if(!wn_help("@")) {                   /* allocate the memory */
  210.     initflg = -2;                       /* DONT ALLOW FURTHER ATTEMPTS */
  211.     return(FALSE);                      /* return if error */
  212.   }
  213.   else {                                /* set up buffers if ok.. */
  214.     return(wn_help(help_file_name));    /* setup help ISAM */
  215.   }
  216. }
  217.  
  218. /*
  219. ***********
  220. * wn_help *
  221. ***********
  222. */
  223.  
  224. wn_help(subject)                        /* help function */
  225. char *subject;                          /* help subject request */
  226. {
  227. char tmpname[80];                       /* temporary filename */
  228. int i;                                  /* scratch integer */
  229.  
  230.   switch(subject[0]) {                  /* on first char of subject ... */
  231.     case '%':                           /* subject page */
  232.       wn1 = wn_open(0, hrow, hcol, hwidth, hheight, hwatr, hbatr);
  233.       if(!wn1) return(FALSE);           /* error return */
  234.       wn_wrap(wn1,FALSE);               /* no wrap thank you */
  235.       v_rcpos(0, &row, &col);           /* remember current location */
  236.       v_hidec();                        /* hide the physical cursor */
  237.       wn_locate(wn1,0,0);               /* home virtual cursor */
  238.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  239.       i=0;                              /* start search index */
  240.       while(i < HLPMAXKY) {             /* look for match in key table */
  241.         if(!strncmp(subject,master[i].key,strlen(subject))) {
  242.           showpage(master[i].loc);      /* display all pages */
  243.           return(TRUE);                 /* return .. all is well */
  244.         }
  245.         i++;
  246.       }
  247.       wn_printf(wn1," Sorry... No info on %s\n", subject);
  248.       wn_printf(wn1," Press any key to continue...");
  249.       v_getch();
  250.       wn_close(wn1);
  251.       return(FALSE);                    /* no match !! */
  252.     case '$':                           /* menu page */
  253.       break;                            /* sometime in the future */
  254.     case '@':                           /* Initialize (allocate memory) */
  255.       for(i=0; i<HLPMAXKY; i++) {
  256.         master[i].key = malloc(HLPKEYLN);  /* keys can be 25 chars long */
  257.         if(!master[i].key) {
  258.           wns_errcod = 3;
  259.           return(FALSE);
  260.         }
  261.       }
  262.       for(i=0; i<hheight+1; i++) {       /* allocate memory for screen bfr */
  263.         scrbuf[i] =  malloc((hlsize+1) * sizeof(char*));
  264.         if(!scrbuf[i]) {
  265.           wns_errcod = 4;
  266.           return(FALSE);
  267.         }
  268.       }
  269.       initflg = TRUE;
  270.       return(TRUE);
  271.     default:                            /* assume help filename */
  272.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  273.       strcpy(tmpname,subject);          /* form filename from root */
  274.       strcat(tmpname,".ndx");
  275.       if(!(fp = fopen(tmpname,RD))) {   /* index file cant be found ! */
  276.         printf("Can't find index file\n");
  277.         printf("Press any key to continue...");
  278.         v_getch();
  279.         return(FALSE);
  280.       }
  281.       i = 0;                            /* read and process key file */
  282.       while(fgets(line,hlsize,fp)) {    /* build the master index table */
  283.         strcpy(master[i].key, line);
  284.         if(line[0] != '%')              /* ignore all but subects */
  285.           continue;
  286.         fgets(line,hlsize,fp);
  287.         master[i].loc = atol(line);
  288.         i++;
  289.       }
  290.       fclose(fp);                       /* close index file */
  291.       strcpy(tmpname, subject);         /* form help file name from root */
  292.       strcat(tmpname, ".hlp");          /* open and leave open ! */
  293.       if(!(fp = fopen(tmpname, RD))) return(FALSE);
  294.       return(TRUE);
  295.   }
  296. }
  297.  
  298. /* End */
  299.